
package com.idealighter.game.games.weishuwu.handler;

import com.google.inject.Inject;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.dic.WeishuwuTimeDic;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.dictionary.domain.WeishuwuRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.games.weishuwu.WeishuwuPlayer;
import com.idealighter.game.games.weishuwu.message.ReqExitTableMsg;
import com.idealighter.game.games.weishuwu.message.ResTimeMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(522225)
public class ResTimeHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResTimeHandler.class);
  private static final int BET = 2;
  private static final int BALANCE = 3;

  @Inject
  WeishuwuTimeDic timeDic;

  @Override
  public void action(Player player, ResMessage message) {
    ResTimeMsg msg = (ResTimeMsg) message;
    WeishuwuPlayer brnnPlayer = (WeishuwuPlayer) player;
    MemInfo member = player.member(player.playerId);
    brnnPlayer.state = msg.getState();
    if (msg.getState() == BET) {
      if (player.playerInfo.getGold() <= 0) {
        player.delayLogout();
        return;
      } else {
        if (member.getChips() == 0) {
          brnnPlayer.exchangeChips(player);
        }
      }
      int betTime = timeDic.get(BET).getTime();
      for (int i = 0; i < RandCodeUtil.random(5, 10); i++) {
        player.schedule(() -> brnnPlayer.dobet(), RandCodeUtil.random(betTime), TimeUnit.SECONDS);
      }

    } else if (msg.getState() == BALANCE) {
      RobotConfigDomain robotCfg = player.configDomain;
      brnnPlayer.totalBet = 0;
      WeishuwuRoomDomain roomDomain = ((WeishuwuPlayer) player).roomDomain;
      // 超过最大携带金币需要退出房间
      if (convertGold(roomDomain, member.getChips()) > robotCfg.getMaxGold()) {
        LOG.info("[魏蜀吴玩家[{}][{}]中身上的金币超过最大携带金币", player.userName, player.playerId,
            robotCfg.getMaxGold());
        player.delayLogout();
        return;
      }
      if (convertGold(roomDomain, member.getGold()) < roomDomain.getLower()) {
        LOG.info("[魏蜀吴玩家[{}][{}]中身上的金币不满足房间的最小金币数[{}]", player.userName, player.playerId,
            roomDomain.getLower());
        // player.delayLogout();
        return;
      }

      // 超过最大游戏局数
      if (player.totalGameNum-- < 1) {
        LOG.info("[魏蜀吴玩家[{}][{}]中超过总游戏局数退出游戏...", player.userName, player.playerId);
        player.delayLogout();
        return;
      }
      LOG.info("[魏蜀吴玩家[{}][{}]结算", player.userName, player.playerId);
      // 超过换桌局数
      if (player.tableGameNum-- < 1) {
        player.tableGameNum =
            RandCodeUtil.random(robotCfg.getTableGameLower(), robotCfg.getTableGameUpper());
        LOG.info("[魏蜀吴玩家[{}][{}]中超过换桌局数换桌...", player.userName, player.playerId);
        player.sendMsg(new ReqExitTableMsg());
      }
    }
  }

  /**
   * 筹码换算金币.
   * 
   * @param roomDomain .
   * @param chips .
   */
  public long convertGold(WeishuwuRoomDomain roomDomain, long chips) {
    return chips * roomDomain.getProportionGold() / roomDomain.getProportionChips();
  }
}
